/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Project;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import javax.annotation.Resource;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Named;
import javax.sql.DataSource;
import javax.sql.rowset.CachedRowSet;

/**
 *
 * @author Luke Nomeland
 */
@Named
@ConversationScoped
public class MessageBean implements Serializable {

    private String message = "";
    private String recipient = "";
    private String subject = "";
    @Resource(name = "jdbc/mydb")
    private DataSource ds;
    private UserBean user;

    public MessageBean() {
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getRecipient() {
        return recipient;
    }

    public void setRecipient(String recipient) {
        this.recipient = recipient;
    }
    
    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public boolean sendMessage() throws SQLException {
        Connection conn = ds.getConnection();
        try {
            Statement stmt = conn.createStatement();
            int newMessage = 1;
            stmt.executeUpdate("INSERT INTO \"MESSAGES\"(USERNAME,SUBJECT,MESSAGE,NEWMESSAGE,SENTBY,TIMESTAMP) VALUES('" + recipient + "','" + subject + "','"+ "','" + message + "','" + newMessage + "','" + user.getUserName() + "','" + MessageBean.getCurrentTime() + "')");
            return true;
        } finally {
            conn.close();
        }
    }

    public ResultSet getMessages() throws SQLException {
        Connection conn = ds.getConnection();
        try {
            Statement stmt = conn.createStatement();
            ResultSet result = stmt.executeQuery("SELECT * FROM \"MESSAGES\" where USERNAME='" + user.getUserName() + "'");
            CachedRowSet crs = new com.sun.rowset.CachedRowSetImpl();
            crs.populate(result);
            return crs;
        } finally {
            conn.close();
        }
    }

    public ResultSet getSentMessages() throws SQLException {
        Connection conn = ds.getConnection();
        try {
            Statement stmt = conn.createStatement();
            ResultSet result = stmt.executeQuery("SELECT * FROM \"MESSAGES\" where SENTBY='" + user.getUserName() + "'");
            CachedRowSet crs = new com.sun.rowset.CachedRowSetImpl();
            crs.populate(result);
            return crs;
        } finally {
            conn.close();
        }
    }

    public ResultSet getNewMessages() throws SQLException {
        Connection conn = ds.getConnection();
        try {
            Statement stmt = conn.createStatement();
            ResultSet result = stmt.executeQuery("SELECT * FROM \"MESSAGES\" where USERNAME='" + user.getUserName() + "'" + "and NEWMESSAGE='1'");
            CachedRowSet crs = new com.sun.rowset.CachedRowSetImpl();      
            crs.populate(result);
            return crs;
        } finally {
            conn.close();
        }
    }
  
    public void readMessage(int messageID) throws SQLException {
        Connection conn = ds.getConnection();
        try {
            Statement stmt = conn.createStatement();
            stmt.executeQuery("UPDATE \"MESSAGES\" SET NEWMESSAGE='0' where MESSAGEID='" + messageID + "'");
        } finally {
            conn.close();
        }
     }
    
    public void deleteMessage(int messageID) throws SQLException {
        Connection conn = ds.getConnection();
        try {
            Statement stmt = conn.createStatement();
            stmt.executeQuery("DELETE FROM \"MESSAGES\" where MESSAGEID='" + messageID + "'");
        } finally {
            conn.close();
        }
     }

    private static Timestamp getCurrentTime() {
        java.util.Date date = new java.util.Date();
        return (new Timestamp(date.getTime()));
    }
}